package cn.yunhe.servlet;

import cn.yunhe.dao.IUserAccount;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 王羊
 */
@WebServlet("/UserAccountServletZC")
public class UserAccountServletZC extends HttpServlet {
    SqlSession sqlSession = null;
    IUserAccount iuserAccount = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //获取输入的注册名和密码和手机号
        String a = request.getParameter("account");
        String b = request.getParameter("password");
        String c = request.getParameter("repassword");
        String d = request.getParameter("tel");
//判断两次输入密码是否相同
        if (b.equals(c)) {
            try{
                sqlSession = SqlSessionFactoryUtil.getSqlSession();
                iuserAccount = sqlSession.getMapper(IUserAccount.class);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String date = sdf.format(new Date());
                Map map = new HashMap();
                map.put("account", a);
                map.put("tel", d);
                //获取输入的短信验证码
                String yan = request.getParameter("yan");
                //获取储存的短信验证码
                String yy = (String) request.getSession().getAttribute("yan");
                int in = iuserAccount.getAllUserAccount(map);
                if (in == 0) {
                    map.put("password", b);
                    map.put("flag", 0);
                    map.put("createdate", date);
                    if (yan.equals(yy)) {
                        boolean bl = iuserAccount.zhuce(map);
                        sqlSession.commit();
                        if (bl) {
                            out.print("<script>alert('注册成功');location.href='register.html';</script>");
                        }
                    } else {
                        out.print("<script>alert('验证码错误');</script>");
                        //输入不正确 跳转至注册页面
                        out.print("<script>location.href='register.html';</script>");
                    }
                } else {
                    //该用户已存在 跳转至注册页面
                    out.print("<script>alert('该用户已存在');location.href='register.html';</script>");
                }
            }finally {
                sqlSession.close();
            }

        } else {
            out = response.getWriter();
            //两次密码输入不一致 跳转至注册页面
            response.setContentType("text/html;charset=UTF-8");
            out.print("<script>alert('两次密码输入不一致');location.href='register.html'</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
